/*导航栏右侧用户信息*/
//import { defineStore } from 'pinia'
import {apiUser} from "@/api/user"
import { UserState } from '@/types'
import { sessionStorage } from "@/utils/storage"

export const useUserStore = defineStore('user',{
    state: (): UserState => ({
        staffId:'',
        staffType:0,
        provinceCode:'9A',
        staffName: '游客',
        avatar: '/img/avatar/avatar.png',
        phoneNum: '18655555555',
        // roles: [],
        // perms: []
        //以上默认值为游客
    }),
    actions:{
        async RESET_STATE() {
            this.$reset()
        },
        getUserInfo(){
            return new Promise((resolve, reject) => {
                apiUser.getUserInfo(false).then((res)=>{
                    const json = res.data
                    this.staffId = json.staffId
                    this.staffType = json.staffType
                    this.provinceCode = json.provinceCode||'9A'
                    this.staffName = json.staffName
                    this.avatar = json.avatar||'/img/avatar/avatar.png'
                    if(json.phoneNum)   this.phoneNum = json.phoneNum
                    resolve(res.data)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        /**
         *  注销
         */
        // logout() {
        //     return new Promise((resolve, reject) => {
        //         apiUser.logout(false).then(() => {
        //             sessionStorage.remove('x-auth-token')
        //             this.RESET_STATE().then(()=>{this.avatar=''})
        //             resetRouter()
        //             resolve(null)
        //         }).catch(error => {
        //             reject(error);
        //         });
        //     });
        // },
        /**
         * 清除 Token ,需根据项目实际情况改写
         */
        resetToken() {
            return new Promise(resolve => {
                localStorage.remove('x-auth-token')
                this.RESET_STATE().then(()=>{})
                resolve(null)
            });
        },
        /**
         * 设置 Token ,需根据项目实际情况改写
         */
        setToken(val:string) {
            localStorage.set('x-auth-token',val)
        },
        /**
         * 读取已存在的 Token ,需根据项目实际情况改写
         */
        getToken() {
            return localStorage.get('x-auth-token')
        },
    },
})